package com.century.sjt.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.century.sjt.R;
import com.century.sjt.fragment.Setting_AnewpwdPayFragment;
import com.century.sjt.fragment.Setting_newpwdPayFragment;
import com.century.sjt.fragment.Setting_oldpwdPayFragment;
import com.century.sjt.util.Constant;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

/**
 * @author:周玲波
 * @Description: 设置支付密码
 * @date : 2016/8/19 16:48
 */
public class Setting_pwdPayActivity extends BaseActivity implements Setting_oldpwdPayFragment.oldClickListener, Setting_newpwdPayFragment.newClickListener {
    private SjtTitleBar titleBar;
    Setting_oldpwdPayFragment oldFragment;
    Setting_newpwdPayFragment newFragment;
    Setting_AnewpwdPayFragment aNewFragment;
    public static SharedPreferences mainSharedPreferences;
    private String isCashPwdSet;
    String oldPwd;
    String newPwd;
    String sn;
    int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_modify_paypws_activity);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.set_modify_pay_title);
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));

        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = getFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                switch (index) {
                    case 2:
                        if (oldFragment == null) {
                            oldFragment = new Setting_oldpwdPayFragment();
                        }
                        transaction.replace(R.id.sjt_pay_fl, oldFragment);
                        transaction.commit();
                        index=0;
                        break;
                    case 3:
                        aNewFragment=null;
                        if (newFragment == null) {
                            newFragment = new Setting_newpwdPayFragment();
                        }
                        transaction.replace(R.id.sjt_pay_fl, newFragment);
                        transaction.commit();
                        index=2;
                        break;
                    default:
                        finish();
                        break;

                }
            }
        });
        mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        isCashPwdSet = mainSharedPreferences.getString("isCashPwdSet", "");

    }

    @Override
    protected void initEvent() {


    }

    @Override
    protected void initData() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!(isCashPwdSet.equals("1"))) {
            if (newFragment == null) {
                newFragment = new Setting_newpwdPayFragment();
            }
            transaction.replace(R.id.sjt_pay_fl, newFragment);
            transaction.commit();
            titleBar.setTitle(getResources().getString(R.string.pwd_new_zf));
        } else {
            if (oldFragment == null) {
                oldFragment = new Setting_oldpwdPayFragment();
            }
            transaction.replace(R.id.sjt_pay_fl, oldFragment);
            transaction.commit();
            titleBar.setTitle(getResources().getString(R.string.pwd_set_zf));
        }

    }

    /**
     * @Description: Setting_oldpwdPayFragment回调
     * @author: 周玲波
     * @date: 2016/8/19 22:16
     */
    @Override
    public void onOldClick(String oldPwd, int index,String sn) {
        this.oldPwd = oldPwd;
        this.index = index;
        this.sn=sn;
        oldFragment=null;
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (newFragment == null) {
            newFragment = new Setting_newpwdPayFragment();
        }
        transaction.replace(R.id.sjt_pay_fl, newFragment);
        transaction.commit();
    }

    /**
     * @Description: Setting_newpwdPayFragment回调
     * @author: 周玲波
     * @date: 2016/8/19 23:18
     */
    @Override
    public void onNewClick(String newPwd, int index) {
        this.newPwd = newPwd;
        this.index = index;
        newFragment=null;
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (aNewFragment == null) {
            aNewFragment = new Setting_AnewpwdPayFragment();
        }
        transaction.replace(R.id.sjt_pay_fl, aNewFragment);
        transaction.commit();
        if(aNewFragment instanceof AnewClickListener){
            if (!(isCashPwdSet.equals("1"))){
                aNewFragment.onANewClick(newPwd);
            }else{
            aNewFragment.onANewClick(oldPwd,newPwd,sn);
            }
        }
    }
    public interface AnewClickListener{
        void onANewClick(String oldPwd,String newPwd,String sn);
        void onANewClick(String newPwd);
    }
}
